package com.kaishun.study.utils;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class ImageUtil {
    /**
     * 图片设置圆角
     *
     * @param srcImage
     * @param radius
     * @param border
     * @param padding
     * @return
     * @throws IOException
     */
    public static BufferedImage setRadius(BufferedImage srcImage, int radius, int border, int padding) throws IOException {
        int width = srcImage.getWidth();
        int height = srcImage.getHeight();
        int canvasWidth = width + padding * 2;
        int canvasHeight = height + padding * 2;

        BufferedImage image = new BufferedImage(canvasWidth, canvasHeight, BufferedImage.TYPE_INT_ARGB);
        Graphics2D gs = image.createGraphics();
        gs.setComposite(AlphaComposite.Src);
        gs.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        gs.setColor(Color.WHITE);
        gs.fill(new RoundRectangle2D.Float(0, 0, canvasWidth, canvasHeight, radius, radius));
        gs.setComposite(AlphaComposite.SrcAtop);
        gs.drawImage(setClip(srcImage, radius), padding, padding, null);
        if (border != 0) {
            gs.setColor(Color.GRAY);
            gs.setStroke(new BasicStroke(border));
            gs.drawRoundRect(padding, padding, canvasWidth - 2 * padding, canvasHeight - 2 * padding, radius, radius);
        }
        gs.dispose();
        return image;
    }

    /**
     * 图片设置圆角
     *
     * @param srcImage
     * @return
     * @throws IOException
     */
    public static BufferedImage setRadius(BufferedImage srcImage) throws IOException {
        int radius = (srcImage.getWidth() + srcImage.getHeight()) / 6;
        return setRadius(srcImage, radius, 2, 5);
    }

    /**
     * 图片切圆角
     *
     * @param srcImage
     * @param radius
     * @return
     */
    private static BufferedImage setClip(BufferedImage srcImage, int radius) {
        int width = srcImage.getWidth();
        int height = srcImage.getHeight();
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        Graphics2D gs = image.createGraphics();

        gs.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        gs.setClip(new RoundRectangle2D.Double(0, 0, width, height, radius, radius));
        gs.drawImage(srcImage, 0, 0, null);
        gs.dispose();
        return image;
    }

    /**
     *
     * @Title: zoomImage
     * @Description: 放大，缩小图片
     * @param @param srcImgFileName 要缩放的图片路径
     * @param @param tagwidth 缩放后图片的宽
     * @param @return
     * @param @throws IOException
     * @return BufferedImage
     * @throws
     */
    public static void zoomImage(String srcImgFileName, int tagwidth, int tagHeight, String outFileName) {
        // 读入文件
        File _file = new File(srcImgFileName);
        // 构造Image对象
        BufferedImage src;
        try {
            src = ImageIO.read(_file);
            int width = src.getWidth();
            int height = src.getHeight();
            height = (tagwidth * height / width);// 等比例算出缩放后的图片高度
            BufferedImage tag = new BufferedImage(tagwidth, tagHeight, BufferedImage.TYPE_INT_RGB);// 绘制tagwidth*height
            // 的图像面板
            tag.getGraphics().drawImage(src, 0, 0, tagwidth, tagHeight, null);// 把src
            // 从tag坐标0,0
            // 开始,绘制成tagwidth*height的图像
            File f = new File(outFileName);
            if (!f.exists()) {
                f.mkdirs();
            }
            ImageIO.write(tag, "jpg", f);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    public static void main(String[] args){
        ImageUtil.zoomImage("/Users/zhoukaishun/Desktop/zks/img/cat2.png",70,70,"/Users" +
                "/zhoukaishun/Desktop/cat123.png");
    }
}

